package com.nuc.kl.module.kpoint.service.impl;

import java.util.Date;

import org.hibernate.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nuc.kl.base.DaoSupportImpl;
import com.nuc.kl.module.kpoint.entity.Knowledge;
import com.nuc.kl.module.kpoint.entity.KnowledgeLog;
import com.nuc.kl.module.kpoint.service.KnowledgeLogService;
import com.nuc.kl.module.usermanager.entity.People;

@Service
@Transactional
public class KnowledgeLogServiceImpl extends DaoSupportImpl<KnowledgeLog> implements KnowledgeLogService{
	
	public void save(String type,Knowledge knowledge, People people) {

		/**
		 *保存知识点的修改日志 
		 */
		KnowledgeLog knowledgeLog = new KnowledgeLog();
		knowledgeLog.setActionTime(new Date());
		knowledgeLog.setActionType(type);
		knowledgeLog.setKnowledge(knowledge);
		knowledgeLog.setPeople(people);
		this.save(knowledgeLog);
	}

	public Long countByKnowledgeId(Long id) {
		
			return  (Long) getSession().createQuery(//
					"SELECT COUNT(*) FROM  KnowledgeLog kl where kl.knowledge.id="+id)//
					.uniqueResult();
	}

	public String getCreatorByKnowledgeId(long id) {
		return (String)getSession().createQuery(
				"SELECT kl.people.name FROM  KnowledgeLog kl where kl.knowledge.id="+id+" and kl.actionType='save'")//
				.uniqueResult();
	}

	public Date getResentTime(long id) {
		
		return (Date)getSession().createQuery(
				"SELECT kl.actionTime FROM  KnowledgeLog kl where kl.knowledge.id="+id+" order by kl.id desc ")//
				.setFirstResult(0)
				.setMaxResults(1).uniqueResult();
	}

	public void deleteByKnowledge(Knowledge k) {
		
		Query q = getSession().createQuery(//
				"delete KnowledgeLog where knowledge.id="+k.getId());
		q.executeUpdate();
	}

	
	
}
